<?php
namespace Framework\Library\Core;

/**
 * 模块控制器基类
 * 
 * @version 1.0
 * @author Canvas Kent
 */
class Module{
	
	/**
	 * 实例化当前模块的应用程序对象
	 * @var Application
	 */
	public $application;
	
	public $defaultAction = NULL;
	
	/** 视图模板数据存储变量 */
    public $viewData;
    
    /**
     * 实例化当前模块的应用程序对象
     * @param Application $appcation
     */
    function __construct( $appcation ){
    	$this ->application = $appcation;
    	if ($this->defaultAction === NULL) $this->defaultAction = $this->application->config->DefaultAction;
    }
    
    /**
     * 未知操作处理
     */
    public function __call( $name , $arguments ){
      
      $this ->assign( 'request_action', $name );
      $this ->assign('message','被请求的操作不存在！');
      $this ->display('NotFound');
      
      header('HTTP/1.1 404 Not Found');
    
    }
    
    /**
     * 向视图模板分派数据
     */
    protected function assign( $key, $value ){
      
      //检查数组是否已初始化
      if ( !is_array($this ->viewData) ) $this ->viewData = array();
      
      $this ->viewData[$key] = $value;
    
    }
    
    /**
     * 用视图模板渲染执行结果
     */
    protected function display( $tpl_name = NULL ){
      
    	require_once 'Template.class.php';
    	$tpl = new Template($this);
    	$tpl ->dispaly($tpl_name);

    }
    
    /**
     * 判断当前请求的方式是否是POST
     */
    public function isPost(){
    	
    	if ( $_SERVER['REQUEST_METHOD']=='POST' ) return true;
    	else return false;
    	
    }
    
    /**
     * 按指定的数据生成URL
     * @param unknown_type $module
     * @param unknown_type $action
     * @param unknown_type $params
     * @return string
     */
    public function url( $module = NULL, $action = NULL, $params = array() ){
    	
    	$url = '';
    	
    	// 创建应用程序链
    	$application = $this->application;
    	$applications = array($application);
    	
    	while ($application ->parent){
    		
    		$application = $application ->parent;
    		array_push($applications, $application);
    		
    	}
    	
    	// 把顺序反转，形成由根应用程序到当前应用程序的排列
    	$application_chain = array();
    	
    	do{
    		
    		$a = array_pop($applications);
    		if ($a) array_push($application_chain,$a);
    		
    	}while($a);
    	
    	// 历遍应用程序链，生成URL
    	for ( $i=0; $i<count($application_chain); $i++ ){
    		
    		$m = $application_chain[$i] ->request ->module;
    		$a = $application_chain[$i] ->request ->action;
    		
    		if ($i == count($application_chain)-1){ // 如果是最后一个应用程序
    			$m = $module;
    			$a = $action;
    		}
    		
    		if ($i == 0) $url .= '?m='.$m.'&a='.$a;    		
    		else $url .= '&p'.(($i*2)-2).'='.$m.'&p'.(($i*2)-1).'='.$a;
    		
    		if ($i == count($application_chain)-1){
    			 
    			$ii = ($i*2);
    			foreach ($params as $param){
    				$url .= "&p$ii=$param";
    				$ii++;
    			}
    			 
    		}
    		
    	}
    	
    	return $url;
    	
    }
    
    /**
     * 使用$this->url生成URL，并跳转至该URL
     * @param unknown_type $module
     * @param unknown_type $action
     * @param unknown_type $params
     */
    public function redirect( $module = NULL, $action = NULL, $params = array() ){
    	header('Location: '.$this->url($module,$action,$params));
    }
    
    public function back(){
    	
    	$url = $_SERVER['HTTP_REFERER'];
    	header('Location: '.$url);
    	
    }
  
  }